package Socket;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Client {
        private Socket socket;

        public Client(){
            try {
                /*
                需要传递两个参数：
                1.服务器的IP地址，用于找到网络上对方的计算机
                2.服务端口
                 */

                System.out.println("正在连接服务器");
                socket = new Socket("localhost",8088);
                System.out.println("与服务器连接成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    public void start(){
//低级流，作用：将写出的字节发送给建立连接的远程计算机

        try {
            PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8)),true);
//            pw.println("你好，服务器！！！");

            Scanner sc = new Scanner(System.in);
            while(true){
                String line = sc.nextLine();
                if("exit".equals(line)){
                    break;
                }
                pw.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }
}
